﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SapLop.Data;
using SapLop.Objects;

namespace SapLop
{
    public partial class frmMain : Form
    {
        private DataView dvChuyenNganhDK;
        private DataView dvChuyenNganhLop;
        private DataView dvNganhCha;
        private DataView dvLop;
        private BindingList<SinhVienOBJ> dsSVLop;
        public frmMain()
        {
            InitializeComponent();
        }       
        
        private void frmMain_Load(object sender, EventArgs e)
        {
            // Lấy dữ liệu ban đầu
            DataAccess da = new DataAccess();
            dvNganhCha = new DataView(da.LayTable("SELECT * FROM NGANH WHERE MANGANHCHA is NULL", DataAccess.KetNoiCSDL()));
            dvChuyenNganhDK = new DataView(da.LayTable("SELECT * FROM NGANH WHERE MANGANHCHA is not NULL", DataAccess.KetNoiCSDL()));
            dvChuyenNganhLop = new DataView(da.LayTable("SELECT * FROM NGANH WHERE MANGANHCHA is not NULL", DataAccess.KetNoiCSDL()));

            cmbBac.DataSource = da.LayTable("SELECT * FROM BAC", DataAccess.KetNoiCSDL());
            cmbBac.DisplayMember = "TENBAC";
            cmbBac.ValueMember = "MABAC";
            cmbBac.SelectedValue = 3;

            cmbNganh.DataSource = dvNganhCha;
            cmbNganh.DisplayMember = "TENNGANH";
            cmbNganh.ValueMember = "MANGANH";

            cmbKhoa.DataSource = da.LayTable("SELECT distinct '20' + NAM as KHOA FROM CTDT", DataAccess.KetNoiCSDL());
            cmbKhoa.DisplayMember = "KHOA";
            cmbKhoa.ValueMember = "KHOA";
        }


        private void cmbBac_SelectedIndexChanged(object sender, EventArgs e)
        {
            dvNganhCha.RowFilter = "MABAC = " + ((DataRowView)cmbBac.SelectedItem)["MABAC"].ToString();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            DataAccess da = new DataAccess();
            string strMaNganhCha = ((DataRowView)cmbNganh.SelectedItem)["MANGANH"].ToString();
            dvChuyenNganhDK.RowFilter = "MANGANHCHA = " + strMaNganhCha;
            dvChuyenNganhLop.RowFilter = "MANGANHCHA = " + strMaNganhCha;
            if (dvChuyenNganhDK.Count > 0)
            {
                string strTVLop = string.Format("SELECT * FROM LOP l INNER JOIN NGANH n ON n.MANGANH = l.MANGANH WHERE l.KHOA = {0} and n.MANGANHCHA = {1} and l.MALHDT='1'", cmbKhoa.Text.Substring(2, 2), strMaNganhCha);
                dvLop = new DataView(da.LayTable(strTVLop, DataAccess.KetNoiCSDL()));
                radTatCa.Checked = true;
                cmbChuyenNganhDK.DataSource = dvChuyenNganhDK;
                cmbChuyenNganhDK.DisplayMember = "TENNGANH";
                cmbChuyenNganhDK.ValueMember = "MANGANH";
                cmbChuyenNganhDK.SelectedIndex = -1;

                cmbChuyenNganhLop.DataSource = dvChuyenNganhLop;
                cmbChuyenNganhLop.DisplayMember = "TENNGANH";
                cmbChuyenNganhLop.ValueMember = "MANGANH";
                cmbChuyenNganhLop.SelectedIndex = -1;
            }
            else
            {
                radTatCa.Checked = false;
            }
        }

        private void radTatCa_CheckedChanged(object sender, EventArgs e)
        {
            if (radTatCa.Checked)
            {
                cmbChuyenNganhDK.Enabled = false;
                DataAccess da = new DataAccess();
                sinhVienOBJBindingSource.DataSource = da.DanhSachSVChuaSapLop();
            }
        }

        private void radChuyenNganh_CheckedChanged(object sender, EventArgs e)
        {
            if (radChuyenNganh.Checked)
            {
                cmbChuyenNganhDK.Enabled = true;
                cmbChuyenNganhDK.SelectedIndex = 0;
                DataRowView dv = (DataRowView)cmbChuyenNganhDK.SelectedItem;
                DataAccess da = new DataAccess();
                sinhVienOBJBindingSource.DataSource = da.DanhSachSVChuaSapLop(dv["MANGANH"].ToString());
            }
        }

        private void cmbChuyenNganhDK_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radChuyenNganh.Checked)
            {
                DataRowView dv = (DataRowView)cmbChuyenNganhDK.SelectedItem;
                DataAccess da = new DataAccess();
                sinhVienOBJBindingSource.DataSource = da.DanhSachSVChuaSapLop(dv["MANGANH"].ToString());
            }
        }

        private void btnAddOne_Click(object sender, EventArgs e)
        {
            if (dgvChuaSapLop.SelectedRows.Count < 0)
                MessageBox.Show("Chưa chọn sinh viên", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            else
            {
                int iTam = dgvChuaSapLop.SelectedRows.Count;
                for (int i = 0; i < iTam; i++ )
                {
                    DataGridViewRow row = dgvChuaSapLop.SelectedRows[0];
                    // Đưa sinh viên vào lớp
                    SinhVienOBJ sv = new SinhVienOBJ();
                    sv.MSSV = row.Cells["Column2"].Value.ToString();
                    sv.Ho = row.Cells["Column3"].Value.ToString();
                    sv.Ten = row.Cells["Column4"].Value.ToString();
                    sv.Phai = row.Cells["Column5"].Value.ToString();
                    dsSVLop.Add(sv);

                    //Remove
                    dgvChuaSapLop.Rows.Remove(row);
                }
            }
        }

        private void cmbChuyenNganhLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbChuyenNganhLop.Text != "")
            {
                DataRowView dv = (DataRowView)cmbChuyenNganhLop.SelectedItem;
                DataAccess da = new DataAccess();
                dvLop.RowFilter = "MANGANH = " + dv["MANGANH"].ToString();
                cmbLop.DataSource = dvLop;
                cmbLop.DisplayMember = "LOP";
                cmbLop.ValueMember = "MALOP";
            }
        }

        private void cmbLop_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataAccess da = new DataAccess();
            DataRowView dv = (DataRowView)cmbLop.SelectedItem;
            dsSVLop = da.DanhSachSVCuaLop(dv["MALOP"].ToString());
            sinhVienOBJBindingSource1.DataSource = dsSVLop;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            DataAccess da = new DataAccess();
            // Lưu vào SINHVIEN_LOP
            DataRowView dv = (DataRowView)cmbLop.SelectedItem;
            string strMaLop = dv["MALOP"].ToString();
            foreach(SinhVienOBJ sv in dsSVLop)
            {
                if (sv.MaLopMoi == "")
                {
                    //da.LuuSinhVienLop(sv.MSSV, strMaLop);
                    sv.MaLopMoi = strMaLop;
                    // Lưu vào SINHVIEN
                    da.LuuSinhVien(sv.MSSV, strMaLop);
                    da.CapNhatXepLop(sv.MSSV);
                }
            }
            MessageBox.Show("Lưu thành công!");
        }
    }
}
